#include <myheader_c_plus.h>
using std::cout;
using std::endl;
//-> *的运算符重载
class Data{
private:
    int _data = 10;
public:
    Data(){}
    ~Data(){}
    friend class MiddleLayer; 
    friend class ThirdLayer;
    int getData(){
        return _data;
    }
};


//第二层级
class MiddleLayer{
private:
    Data * _pdata;
public:
    MiddleLayer(Data *p)
    :_pdata(p)
    {

    }

    ~MiddleLayer(){
        if(_pdata != nullptr){
            delete _pdata;
            _pdata = nullptr;
        }
    }
    
    int getData(){
        return _pdata->getData();

    }

    //对->运算符重载
    Data * operator->(){
        return _pdata;
    }

    //对*运算符重载
    Data & operator*(){
        return *_pdata;
    }

};


//引入第三层结构
class ThirdLayer{
private:
    MiddleLayer *_ml;
public:
    //构造函数
    ThirdLayer(MiddleLayer *ml)
    :_ml(ml)
    {

    }

    //析构函数
    ~ThirdLayer(){
        if(_ml !=nullptr){
            delete _ml;
            _ml = nullptr;
        }
    }

    //对->进行运算符重载，因为可以递归，所以直接返回*ml即可
    MiddleLayer * operator->(){
        return _ml;
    }

    //对*进行运算符重载
    Data & operator*(){
        return *(*_ml);
    }

};
int main(int argc, char *argv[])
{
    ThirdLayer tl(new MiddleLayer(new Data));
    cout << tl->getData() << endl;
    cout << (*tl).getData() << endl;
    return 0;
}

